package com.lishui.waterfriend.controller;

import com.lishui.waterfriend.annotation.JudgeEmpty;
import com.lishui.waterfriend.common.result.PageResult;
import com.lishui.waterfriend.common.result.Result;
import com.lishui.waterfriend.contant.MessageConstant;
import com.lishui.waterfriend.enums.OperationTypeEnum;
import com.lishui.waterfriend.exception.BusinessException;
import com.lishui.waterfriend.pojo.dto.*;
import com.lishui.waterfriend.pojo.entity.Team;
import com.lishui.waterfriend.pojo.vo.TeamUserVO;
import com.lishui.waterfriend.service.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/team")
@Api(tags = "队伍相关接口")
@Slf4j
public class TeamController {
    @Resource
    private TeamService teamService;

    @PostMapping
    @ApiOperation("创建队伍")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result<Long> save(@RequestBody TeamSaveDTO teamSaveDTO) {
        Team team = new Team();
        BeanUtils.copyProperties(teamSaveDTO, team);
        Long teamId = teamService.saveTeam(team);
        if (team == null) throw new BusinessException(MessageConstant.SAVE_EXCEPTION + ":team");
        return Result.success(teamId);
    }

    @GetMapping
    @ApiOperation("队伍分页查询")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result<PageResult> pageQuery(TeamQueryDTO teamQueryDTO) {
        PageResult pageResult = teamService.pageQuery(teamQueryDTO);
        return Result.success(pageResult);
    }

    @PutMapping
    @ApiOperation("修改队伍信息")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result update(@RequestBody TeamUpdateDTO teamUpdateDTO) {
        teamService.updateTeam(teamUpdateDTO);
        return Result.success();
    }

    @PostMapping("/join")
    @ApiOperation("用户加入队伍")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result join(@RequestBody TeamJoinDTO teamJoinDTO) {
        teamService.join(teamJoinDTO);
        return Result.success();
    }

    @PostMapping("/quit/{id}")
    @ApiOperation("用户退出退伍")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result quit(@PathVariable Long id) {
        teamService.quit(id);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("队长解散队伍")
    @JudgeEmpty(OperationTypeEnum.YES)
    public Result remove(@PathVariable Long id) {
        teamService.disband(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据Id获取队伍信息")
    public Result<TeamUserVO> getTeamById(@PathVariable Long id) {
        Team team = teamService.checkTeamId(id);
        TeamUserVO teamUserVO = new TeamUserVO();
        BeanUtils.copyProperties(team, teamUserVO);
        return Result.success(teamUserVO);
    }

}
